﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RuiJing.Web.Areas.Manage.Models.ManagerModels;
using Code.Models;
using Code.BLL;
using Code.Commen;
using RuiJing.Web.Commen;

namespace RuiJing.Web.Areas.Manage.Controllers
{
    public class ManagerController : ControllerBase
    {
        ManagerManage MM = new ManagerManage();
        public ActionResult Index()
        {
            var data = MM.GetTable<Admin>().Select(p => new AdminItem { ID = p.ID, LoginName = p.LoginName, State = (AdminState)p.State }).ToArray();
            data.Each(p => { p.InLine = InlineAdmin.ContainsKey(p.ID); p.IsCurrent = p.ID == Admin.ID; });
            var model = new IndexModel
            {
                Admins=data,
            };
            return View(model);
        }

        public ActionResult Add()
        {
            var model = new AdminEditModel();
            return View(model);
        }

        [HttpPost]
        public ActionResult Add([Bind(Exclude="ID")]AdminEditModel model)
        {
            if (string.IsNullOrEmpty(model.Password))
                ModelState.AddModelError("Password", "请输入登录密码");
            if (string.IsNullOrEmpty(model.ConfirmPwd))
                ModelState.AddModelError("ConfirmPwd", "请输入确认密码");
            if (model.Password!=model.ConfirmPwd)
                ModelState.AddModelError("ConfirmPwd", "两次输入不一至");

            try
            {
                MM.CheckPwd(Admin.LoginName, model.CheckPwd);
            }
            catch (AppException)
            {
                ModelState.AddModelError("CheckPwd", "验证密码不正确");
            }

            if (ModelState.IsValid)
            {
                try
                {
                    var admin = new Admin
                    {
                        LoginName = model.LoginName,
                        Password = model.Password,
                    };
                    int rs = MM.Add(admin);
                    if (rs > 0)
                        return Content(Url.Action("index").Redirect("新增成功！"));
                    else
                        ViewData["msg"] = "操作失败，请稍后再试！";
                }
                catch (ValidateException e)
                {
                    ModelState.AddModelError(e.Source, e.Message);
                }
            }
            return View(model);
        }

        public ActionResult Edit(Guid? id)
        {
            if (id == null)
                return Content(Url.Action("index").Redirect());
            var a = MM.GetTable<Admin>().FirstOrDefault(p => p.ID == id);
            if (a == null)
                return Content(Url.Action("index").Redirect("用户不存在"));

            var model = new AdminEditModel
            {
                ID=id,
                LoginName=a.LoginName,
            };
            return View(model);
        }

        [HttpPost]
        public ActionResult Edit(AdminEditModel model)
        {
            if (model.ID == null)
                return Content(Url.Action("index").Redirect());
            if (!string.IsNullOrEmpty(model.Password))
            {
                if (string.IsNullOrEmpty(model.ConfirmPwd))
                    ModelState.AddModelError("ConfirmPwd", "请输入确认密码");
                if (model.Password != model.ConfirmPwd)
                    ModelState.AddModelError("ConfirmPwd", "两次输入不一至");
            }
            try
            {
                MM.CheckPwd(Admin.LoginName, model.CheckPwd);
            }
            catch (AppException)
            {
                ModelState.AddModelError("CheckPwd", "验证密码不正确");
            }
            if (ModelState.IsValid)
            {
                try
                {
                    var admin = new Admin
                    {
                        LoginName = model.LoginName,
                        Password = model.Password,
                        ID=model.ID.Value,
                    };
                    int rs = MM.Edit(admin);
                    if (rs > 0)
                    {
                        if (base.InlineAdmin.ContainsKey(admin.ID))
                            base.InlineAdmin[admin.ID].Reload = true;
                        return Content(Url.Action("index").Redirect("编辑成功！"));
                    }
                    else
                        ViewData["msg"] = "操作失败，请稍后再试！";
                }
                catch (ValidateException e)
                {
                    ModelState.AddModelError(e.Source, e.Message);
                }
                
            }
            return View(model);
        }

        public ActionResult Del(Guid id)
        { 
            string msg;
            if (Admin.ID == id)
                msg = "您不能删除自己";
            else
            {
                int rs = MM.Del(id);
                if (rs > 0)
                {
                    if (base.InlineAdmin.ContainsKey(id))
                        base.InlineAdmin[id].Reload = true;
                    msg = null;
                }
                else if (rs == 0)
                    msg = "未找到该用户";
                else
                    msg = "操作失败,请稍后在试.";
            }
            return Content(Url.Action("index").Redirect(msg));
        }

        public ActionResult State(Guid a,AdminState id)
        {
            string msg;
            if (Admin.ID == a)
                msg = "您不能修改自己";
            else
            {
                int rs = 0;
                if (id == AdminState.Disabled || id == AdminState.Enabled)
                    rs = MM.State(a, id);
                if (rs > 0)
                {
                    if (base.InlineAdmin.ContainsKey(a))
                    base.InlineAdmin[a].Reload = true;
                    msg = null;
                }
                else if (rs == 0)
                    msg = "未找到该用户";
                else
                    msg = "操作失败,请稍后在试.";
            }
            return Content(Url.Action("index").Redirect(msg));
        }
    }
}
